package cn.zyjblogs.netty.heartbeat;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleStateEvent;

/**
 *
 */
public class MyServerHandler  extends ChannelInboundHandlerAdapter {
    /***
     * 事件
     * @param ctx 上下文
     * @param evt 事件
     * @throws Exception
     */
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
       if (evt instanceof IdleStateEvent){
           //将evt 向下转型 IdleStateEvent
           IdleStateEvent event = (IdleStateEvent) evt;
           String eventType = null;
           switch (event.state()){
               case READER_IDLE:
                   eventType = "读空闲";
                   break;
               case WRITER_IDLE:
                   eventType = "读空闲";
                   break;
               case ALL_IDLE:
                   eventType = "读写空闲";
                   break;
               default:
                   eventType = "类型未识别";
                   break;
           }
           System.out.println(ctx.channel().remoteAddress()+"--超时类型--"+eventType);
           System.out.println("服务器做响应处理....");
           //如果发生空闲，我们关闭通道
           ctx.close();
       }
    }
}
